const postModel = require('../model/postModel');
const moment = require('moment');
module.exports = {
  getPostsByQuery(req, res) {
    let response = {
      code: 0,
      message: 'error'
    };
    let currentPage = req.body.currentPage;
    let pageSize = req.body.pageSize;
    let category_id = req.body.category_id;
    let status = req.body.status;
    postModel.queryPost(currentPage, pageSize, category_id, status, (err, result) => {
      if (err) throw err;
      postModel.getCountByQuery(category_id, status, (err, countResult) => {
        if (err) throw err;

        result.forEach(e => {
          e.created = moment(e.created).format('YYYY年MM月DD日 HH:mm:ss')
        });

        response.code = 1;
        response.message = 'ok';
        response.data = result;
        response.pageTotal = Math.ceil(countResult[0].total / pageSize)

        res.json(response);
      })

    })
  }
}